import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        // 文件路径
        String directorPresenterFilePath = "src/1983-2023_director_presenter.txt";
        String chineseYearFilePath = "src/1983-2023_chinese_year.txt";

        // 读取导演和主持人数据
        List<String> directorPresenterData = readFile(directorPresenterFilePath);
        // 读取节目数据
        List<String> chineseYearData = readFile(chineseYearFilePath);

        // 统计导演次数
        Map<String, Integer> directorCounts = countDirectors(directorPresenterData);
        // 输出前五名导演及执导次数
        System.out.println("导演次数最多的前五名导演及执导次数：");
        printTopDirectors(directorCounts, 5);

        // 统计主持次数
        Map<String, Integer> hostCounts = countHosts(directorPresenterData);
        // 输出前十名主持人及主持次数
        System.out.println("\n主持春晚次数最多的前十名主持人及主持次数：");
        printTopHosts(hostCounts, 10);
    }

    // 读取文件并返回每一行的数据
    private static List<String> readFile(String filePath) {
        List<String> data = new ArrayList<>();
        Charset charset = Charset.forName("GBK");
        try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(filePath)), charset))) {
            String line;
            while ((line = br.readLine()) != null) {
                data.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    // 统计导演次数
    private static Map<String, Integer> countDirectors(List<String> data) {
        Map<String, Integer> directorCounts = new HashMap<>();
        for (String line : data) {
            String[] parts = line.split("\t"); // 假设数据是以制表符分隔的
            if (parts.length >= 2) {
                String director = parts[1];
                directorCounts.put(director, directorCounts.getOrDefault(director, 0) + 1);
            }
        }
        return directorCounts;
    }

    // 输出前N名导演及执导次数
    private static void printTopDirectors(Map<String, Integer> directorCounts, int topN) {
        directorCounts.entrySet().stream()
                .sorted((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()))
                .limit(topN)
                .forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
    }

    // 统计主持次数
    private static Map<String, Integer> countHosts(List<String> data) {
        Map<String, Integer> hostCounts = new HashMap<>();
        for (String line : data) {
            String[] parts = line.split("\t"); // 假设数据是以制表符分隔的
            if (parts.length >= 3) {
                String hosts = parts[2];
                String[] hostNames = hosts.split("、");
                for (String host : hostNames) {
                    hostCounts.put(host, hostCounts.getOrDefault(host, 0) + 1);
                }
            }
        }
        return hostCounts;
    }

    // 输出前N名主持人及主持次数
    private static void printTopHosts(Map<String, Integer> hostCounts, int topN) {
        hostCounts.entrySet().stream()
                .sorted((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()))
                .limit(topN)
                .forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
    }
}
